﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;
using System.Data;

namespace Sispe.Repository
{
    public class PostulanteRepository:MasterRepository, IPostulanteRepository
    {
        public List<POSTULANTE> GetAllFromPostulante()
        {
            var query = from e in context.POSTULANTEs.Include("PERSONA")
                        select e;
            return query.ToList();
        }

        public List<POSTULANTE> GetAllFromPostulanteByNombre(string nombre)
        {
            var query = from e in context.POSTULANTEs.Include("PERSONA")
                        select e;
            if (nombre != null && nombre != string.Empty && nombre != "")
            {
                query = from e in context.POSTULANTEs
                        where e.PERSONA.Nombres.ToUpper().Contains(nombre)||
                        e.PERSONA.ApeMaterno.ToUpper().Contains(nombre)||
                        e.PERSONA.ApePaterno.ToUpper().Contains(nombre)
                        select e;
            }
            return query.ToList();
        }

        public POSTULANTE GetFromPostulanteByIdPersona(int id)
        {
            var query = from e in context.POSTULANTEs
                        where e.IdPostulante.Equals(id)
                        select e;
            return query.SingleOrDefault();
        }

        public void AddPostulante(POSTULANTE postulante)
        {
            postulante.PERSONA = null;
            postulante.EXPERIENCIA_POSTULANTE = null;

            context.POSTULANTEs.Add(postulante);
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void UpdatePostulante(POSTULANTE postulante)
        {
            postulante.PERSONA = null;
            postulante.EXPERIENCIA_POSTULANTE = null;
            context.Entry(postulante).State = System.Data.EntityState.Modified;
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void RemovePostulante(int id)
        {
            var existe = context.POSTULANTEs.Find(id);

            if (existe != null)
            {
                context.POSTULANTEs.Remove(existe);
                context.SaveChanges();
            }
        }


        public bool ExisteDuplicidad(Int32 id)
        {
            var query = from e in context.POSTULANTEs
                        where e.PERSONA.IdPersona != id
                        select e;
            return query.Any();
        }
    }
}
